package com.supermap.desktop.develop.workspacemigration.internalhelpers;

import com.supermap.data.EngineType;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.develop.workspacemigration.constants.SQLConstants;
import dm.jdbc.driver.DmdbConnection;

import java.sql.*;

public class SQLHelper {

    public static String getDatabaseDriver(EngineType engineType) {
        String databaseDriver = "";

        if (engineType == EngineType.POSTGRESQL || engineType == EngineType.PGGIS) {
            databaseDriver = SQLConstants.PostgreSQLDriver;
        } else if (engineType == EngineType.DM) {
            databaseDriver = SQLConstants.DMDriver;
        }

        return databaseDriver;
    }

    public static String getJDBCDriver(EngineType engineType) {
        String jdbcDriver = "";

        if (engineType == EngineType.POSTGRESQL || engineType == EngineType.PGGIS) {
            jdbcDriver = SQLConstants.JDBCPostgreDriver;
        } else if (engineType == EngineType.DM) {
            jdbcDriver = SQLConstants.JDBCDMDriver;
        }

        return jdbcDriver;
    }

    public static boolean createDMDatasourceUser(String ip, String port, String userName, String password, String datasourceName) {
        boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
        String tablespaceFolder = isWindows ? "C:\\dmdbms\\data\\DAMENG" : "/home/data/DAMENG";
        String tablespaceName = SQLConstants.SuperMap.concat("_").concat(datasourceName.toUpperCase());
        String createTablespaceSQL = "create tablespace \"".concat(tablespaceName).concat("\" datafile '")
                .concat(tablespaceFolder).concat((isWindows ? "\\" : "/"))
                .concat(tablespaceName).concat(".DBF' size 32 autoextend on next 64 maxsize 16777215 CACHE = NORMAL;");
        String createUserSQL = "create user \"".concat(tablespaceName).concat("\" identified by \"").concat(tablespaceName)
                .concat("\" default tablespace \"").concat(tablespaceName).concat("\";");
        String grantSQL = "grant \"DBA\",\"PUBLIC\",\"RESOURCE\",\"SOI\",\"VTI\" to \"".concat(tablespaceName).concat("\";");

        try {
            Class.forName(SQLHelper.getDatabaseDriver(EngineType.DM));
            String url = SQLHelper.getJDBCDriver(EngineType.DM).concat(ip).concat(":").concat(port);
            Connection connection = DriverManager.getConnection(url, userName, password);
            if (!connection.isClosed()) {
                DmdbConnection dmdbConnection = (DmdbConnection) connection;
                Statement statement = dmdbConnection.createStatement();
                statement.execute(createTablespaceSQL);
                statement.execute(createUserSQL);
                statement.execute(grantSQL);
                statement.close();
                dmdbConnection.close();
            }
            connection.close();
        } catch (ClassNotFoundException | SQLException e) {
            Application.getActiveApplication().getOutput().output(e);
        }

        return false;
    }
}
